Solving 10385 - Duathlon (Ternary search)
[andmenj-acm.git] / 10482 - The candyman can / jesus.federico.cpp
blob32088689081343dbd37fe7aafa01f1b617430151
1 #include<iostream>
3 using namespace std;
5 int n,CASO,T[100],mem[55][55][55][40],INF;
7 int dp(int a, int b, int c, int voy)
9 if(a>51||b>51||c>51)
10 return 1<<30;
12 if(voy==n)
13 return max(a,max(b,c));
15 if(mem[a][b][c][voy]!=INF)
16 return mem[a][b][c][voy];
18 int na,nb,nc,mimin,s=1<<30;
20 na=a,nb=b,nc=c;
21 na+=T[voy];
22 mimin=min(na,min(nb,nc));
23 s<?=dp(na-mimin,nb-mimin,nc-mimin,voy+1);
25 na=a,nb=b,nc=c;
26 nb+=T[voy];
27 mimin=min(na,min(nb,nc));
28 s<?=dp(na-mimin,nb-mimin,nc-mimin,voy+1);
30 na=a,nb=b,nc=c;
31 nc+=T[voy];
32 mimin=min(na,min(nb,nc));
33 s<?=dp(na-mimin,nb-mimin,nc-mimin,voy+1);
35 return mem[a][b][c][voy]=s;
38 int main()
40 cin>>CASO;
42 for(int i=1;i<=CASO;i++)
44 cin>>n;
46 for(int j=0;j<n;j++)
47 cin>>T[j];
49 memset(mem,-1,sizeof(mem));
50 INF=mem[0][0][0][0];
52 printf("Case %d: %d\n", i, dp(0,0,0,0));
55 return 0;